# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("QTableWidget示例")
        self.resize(600, 800)
        conLayout = QHBoxLayout()
        tableWidget = QTableWidget()
        tableWidget.setRowCount(30)
        tableWidget.setColumnCount(4)
        conLayout.addWidget(tableWidget)

        for i in range(30):
            for j in range(4):
                itemContent = "(%d,%d)" % (i, j)
                tableWidget.setItem(i, j, QTableWidgetItem(itemContent))
        self.setLayout(conLayout)

        # 遍历表格查找对应项
        text = "(10,1)"
        items = tableWidget.findItems(text, Qt.MatchExactly)
        item = items[0]

        # 选中单元格
        # item.setSelected(True)

        # 设置单元格的背景颜色为红色
        item.setForeground(QBrush(QColor(255, 0, 0)))

        row = item.row()
        # 通过鼠标滚轮定位，快速定位到第11行
        tableWidget.verticalScrollBar().setSliderPosition(row)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exit())